package com.ler.yrmall.product.controller;

import com.ler.yrmall.basic.Result;
import com.ler.yrmall.product.entity.CategoryBrandRelationEntity;
import com.ler.yrmall.product.service.CategoryBrandRelationService;
import com.ler.yrmall.product.vo.CategoryBrandRelationVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;


/**
 * @author Leron
 */
@RestController
@RequestMapping("/categoryBrandRelation")
public class CategoryBrandRelationController {

    @Resource
    private CategoryBrandRelationService categoryBrandRelationService;


    /**
     * 根据品牌/分类 id 获取关联的品牌/分类
     */
    @GetMapping("/category/list")
    public Result<?> categoryList(CategoryBrandRelationEntity request){
        List<CategoryBrandRelationVO> list = categoryBrandRelationService
                .listByBrandId(request);
        return Result.ok(list);
    }


    @RequestMapping("/save")
    public Result<?> save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation) {
        categoryBrandRelationService.saveRelation(categoryBrandRelation);
        return Result.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public Result<?> update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation) {
        categoryBrandRelationService.updateById(categoryBrandRelation);
        return Result.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public Result<?> delete(@RequestBody Long[] ids) {
        categoryBrandRelationService.removeByIds(Arrays.asList(ids));
        return Result.ok();
    }

    @GetMapping("/brands/list")
    public Result<?> categoryBrandRelation(@RequestParam String categoryId){
        return Result.ok(categoryBrandRelationService.getBrandByCategoryId(categoryId));
    }

}
